Deep learning based optimal energy management for photovoltaic and battery energy storage integrated home micro-grid system

The development of the advanced metering infrastructure (AMI) and the application of artificial intelligence (AI) enable electrical systems to actively engage in smart grid systems. Smart homes with energy storage systems (ESS) and renewable energy sources (RES)-known as home microgrids-have become a critical enabling technology for the smart grid. This article proposes a new model for the energy management system of a home microgrid integrated with a battery ESS (BESS). The proposed dynamic model integrates a deep learning (DL)-based predictive model, bidirectional long short-term memory (Bi-LSTM), with an optimization algorithm for optimal energy distribution and scheduling of a BESS-by determining the characteristics of distributed resources, BESS properties, and the user’s lifestyle. The aim is to minimize the per-day electricity cost charged by time-of-use (TOU) pricing while considering the day-basis peak demand penalty. The proposed system also considers the operational constraints of renewable resources, the BESS, and electrical appliances. The simulation results from realistic case studies demonstrate the validation and responsibility of the proposed system in reducing a household’s daily electricity cost.

Contributions. This study proposes a novel scheme for a HEMS that optimally schedules and manages the PV-BESS for the customer in a dynamic environment. This study is the first (to the best of our knowledge) to simulate the integration of a predictive model with a control algorithm for optimal scheduling and maximizing the discharge amount by including dynamic tariffs. Significant contributions of this study are as follows: • In this study, we designed and developed a Bi-LSTM model for day-ahead energy consumption and generation forecasts (described in "Controlling mechanism" section). Training with the predictive model ensures consistent day-ahead forecasting performance by diminishing prediction errors. • This study proposes a heuristic strategy that considers the impact of predicted energy consumption and generation. The real-time scheduling and optimization technique is modeled in a specific interval by considering the energy demand and generation profile (described in "Simulation study" section). • The proposed system is evaluated under different scenarios of day-ahead power consumption and PV generation volume by the consumer, whereas existing studies focus only on the present-day scenario. The proposed system's payback periods are also assessed.
This paper is organized as follows. "System modeling" section describes the modeling of the proposed system. "Controlling mechanism" section presents the control mechanism and operational constraints for the HEMS model. The simulation results and the corresponding discussion are presented in "Simulation study" section, and the conclusion is presented in "Conclusion" section. Figure 1 presents the proposed architecture of the home microgrid system. The home is equipped with different appliances, an AMI, and a BESS integrated with PV panels. The BESS is used to store and supply energy based on different constraints. The AMI system is installed, acting as an information provider to the server. The database stores historical data on power generation and consumption used for forecasting. The constraints of the appliances, the BESS, the RES, and the forecasted results are received as inputs in the optimization model at regular intervals. The model also receives the dynamic electricity tariff information at the same intervals. The output of the optimization model provides the scheduling time and amount of power. In this section, the working principle of the Bi-LSTM network and the control algorithm for the ESS are explained.

Energy consumption and generation forecasting model. An improved variant of the RNN, known
as an LSTM network 35 , removes those limitations by incorporating memory cells and several control gates. Memory cells enable LSTM networks to exploit the long-term dependency of temporal sequences and ensure information propagation through consecutive time steps within internal network structures 36 . Figure 2 presents the LSTM single cell structure consisting of three gates (input gate, output gate, and forget gate). However, an LSTM layer comprises N L -connected single cells. Let X t be the measured PV generated power or energy consumption sample at time step t. The relationship between the actual and the previously observed data was formulated as follows to predict the 24-h-ahead response of the PV generated power or energy consumption: where t ∈ [k, N − 1], k is the time lag, and N is the size of the data. In the equation, LSTM(·) represents the LSTM function of each single cell L ∈ [1, N L ] and is governed by the following: (1) (Ŷ t+1 ,Ŷ t+2 , ...,Ŷ t+24 ) = LSTM(X t−k+1 , ..., X t−1 , X t )  where i t , f t , and o t are the input gate, forget gate, and output gate. Furthermore, g t is used to update the input signal by modifying the memory state, and c t is the cell state value. However, each gate produces an output based on its individual weight matrix and bias term. Consequently, h t is measured with a concatenating cell state value with the output gate value and i s the cell output value. The sigmoid activation function transforms each gate value into a value between 0 and 1. The cell output finally passes through the hyperbolic tangent activation function (tanh) and predicts h t . The unidirectional LSTM model processes the input sequence data at each time step t using the information contained in the past, ignoring future input-an issue that affects forecasting accuracy in multiple applications. This study adopted a bidirectional learning method that explores both the past (before t) and future (after t) temporal information among the entire sequence to boost the accuracy of conventional LSTM networks . The principle of this bidirectional learning process exploring both forward and backward sequence directions by two LSTM layers is illustrated in Fig. 3.
The output response at t using a hidden vector derived from two LSTM layers is calculated as follows: The day-ahead power generation and consumption is necessary for scheduling PV-BESS and optimizing the energy charging and discharging allowances. However, the following is a description of the procedure for determining day-ahead power generation and consumption:   AD scenario. The focus of demand response modeling is cost minimization and user satisfaction. In developing the demand response, the demands of the home microgrid are grouped into different categories depending on the extent to which the demand can be controlled. In this case, the AD of the system is modeled with linear and nonlinear functions according to the properties of the particular user. The dynamic and predicted AD of the household are P H AD,t (t) and P H FAD,t (t) . The total power consumption in a household at time t ∈ τ can be expressed as follows: where ζ ad (t) is the activity status of the appliances at time t. The statuses ζ ad (t) = [0, 1] and ζ ad (t) ∈ Z vary with continuous changes in demand. For n ∈ N households in a building, the total power consumption (i.e., predicted) in the period t ∈ τ (i.e., on this day) is defined as follows: where t = [t start , t end ] , t ∈ Z , s ∈ S , e ∈ E , and k ∈ K The total predicted power that should be consumed and received by the building from t to t end and t start to t are as follows: Because the system has already consumed power from the beginning to time t, the actual consumed power and the day-ahead energy consumption of the system are defined as follows: where the time-ahead factors are a ∈ Z , {t s , t m , t h } ∈ ta sa ∈ SA , and ea ∈ EA.  (15) and (16) describe the total predicted demand profile and consumed power. Therefore, the total day and day-ahead forecasted demand are calculated in Eqs. (16) and (18). Equations (17) and (19) define the average day and day-ahead forecasted power demand.
Distributed generation resource scenario. Consider a residential building with RES, such as a PV power system, where the maximum output power of the PV module is significantly related to its efficiency. In this case, the power generation and the day-ahead power generation are E PV G,t (t) and E PV FPG,t (t) . Because the PV power generation depends significantly on the duration of sunlight, the power generated by n ∈ N number of PV modules at a specific moment is modeled as follows: where ξ PV (t) is the generation status of the PV panel at time t. Status ξ PV (t) ∈ [1, 0] varies with the continuous response of the PV generation data. The total predicted power generation of the present day is defined as follows: The total predicted power and actual generated power are calculated in Eqs. (22) and (23). The total dayahead energy generation profile is the summation of generation at time t to the day-ahead generation end time and is expressed as follows: where sa PV ∈ SA and ea PV ∈ EA . The predicted generated power at t to t end and the average predicted generated power at that time are calculated as follows: www.nature.com/scientificreports/ The day-ahead predicted generated power and their average amount are calculated in Eqs. (26) and (28).
BESS scenario. The objective function for optimal energy management and scheduling in the BESS integrated system aims to maximize reliability and minimize the energy cost of the user. The proposed system focuses on several HERs connected in a building. The operational constraints of ESS in different stages of the optimization formulation are calculated by Eqs. (29) and (30). Consider that n ∈ N number of BESS units are deployed in the system. The instant volume of storage and initial energy can be determined as follows: where ξ PV (t) ∈ [1, 0] and SoC BESS t,id ∈ SoC BESS t,id . The amount of energy that can be supplied to the system and stored in the BESS at time t can be defined as follows: Therefore, constraints for the beginning of charging (such as a particular level of generation) are considered. The set of this threshold level of PV panel generation is defined as E PV G,th . If multiple panels connected with a single ESS are considered, then the sum of the threshold level is defined as follows: where S ESS t,c are binary variables expressing the charging and discharging status of the ESS. Because the charging and discharging process will not occur simultaneously, the appliance will draw power from the grid in the charging period as calculated in Eq. (38).
Electricity price modeling. The per-unit prices of electricity and DCT depend on the volume of the contracted load, the level of the supply voltage, the types of consumers, and their locations. Because this study focuses on residential consumers, demand charge and energy charge are considered. From this perspective, the www.nature.com/scientificreports/ per-unit cost is determined based on the market price of an electricity company 37 . The constraints for the DCT can be expressed as follows: Therefore, a fixed TOU tariff is considered 38 , in which electricity is priced in three different fixed intervals (i.e., off-peak, medium peak, and peak) in a day, including weekdays and weekends, as depicted in Fig. 4. The hourly stepped price signal is defined as follows: where T op t,TOU , T mp t,TOU , and T p t,TOU are the tariff amounts in the off-peak, medium peak, and peak times.

Controlling mechanism
The models of energy demand, distributed generation resources (i.e., PV), and BESS are designed with different control approaches to reduce the demanded energy from the grid and use the generated power appropriately. The constraints of a BESS and dynamic pricing system are also formulated to ensure lower electricity costs. In this study's proposed model, it is assumed that consumers draw energy from the main grid. Furthermore, the DG is deployed to the systems with an integrated BESS. The control approaches are classified by considering their given constraints. After each iteration, a specific constraint-handling mechanism is applied to each control mode. Therefore, in the evaluation process of the proposed algorithm, after each iteration, the constraints for scheduling and discharging are updated. The fundamental condition for supplying power is defined as follows: If the PV generation and the SoC range constraints of the BESS are violated in the given charging period, the charging and discharging schedules of the BESS in that time will be adjusted with updated boundary values. We focused on three control modes for an optimal solution by considering different constraints, as follows.

Primary control mode (PCM). The primary control mode (PCM) is a fundamental control approach for
scheduling the BESS where the supplying power is controlled under specific constraints (time function). It provides minimum discharging allowance varies with forecasted average demand within limited constraints as follows:  where F SCM Dis,t is the secondary discharging factor. If the system's AD is higher than the average day-ahead power prediction, the volume of energy supply to the system will differ. The constraints for this particular period are:   www.nature.com/scientificreports/ Tertiary control mode (TCM). In this case, the additional benefits of the systems are encouraged based on the tariff plan. The discharging process of the BESS will be designed in accordance with the energy consumption, generation, and dynamic pricing of electricity cost. The main feature of this optimization mode is to provide a higher amount of energy to the load during periods of higher tariffs.
where T TCM Dis,Fac,t is the tertiary discharging factor. The objective function for three control mode is formulated as follows: The step-by-step process of the proposed optimization algorithm is described in Algorithm 1.

Simulation study
The real-time energy demand data of the multiple-unit building has been measured by a smart meter, and the PV generation data has been measured from the PV site. The duration of historical data was more than 6 months. The scheduling process starts at 12:00 a.m., continues until 12:00 p.m., and consists of 96 time intervals (every 15 min). The dynamic tariff, including peak, medium peak, and non-peak, are presented in Fig. 4. The proposed forecasting algorithm is implemented in the Python platform. The integration of one-day-ahead energy consumption  The key performance indicators of the forecasting algorithm for multi-step day-ahead prediction and PV power generation are presented in Tables 1 and 2. However, we observed a weekly repeating pattern after analyzing the power consumption profile. Therefore, the predictive model has been trained based on 672 previous time-steps data to predict 96 steps ahead of the power consumption data. In Table 1, the performance in terms of the mean absolute percentage error (MAPE) is 13.69% for H 3 , which is the lowest value among the households.  In contrast, the forecasting of day-ahead PV power generation is performed by measuring 96 time-steps of back power generation data. The performance of the forecasting model in terms of MAPE for H 4 is higher than the other households (MAPE = 16.421% ). The lowest MSE value is from H 2 and H 3 (.0001 kW). Furthermore, the MAE value of H 1 is very lower when compared with the others. Table 3 presents the total day and day-ahead  www.nature.com/scientificreports/ demand, PV generation, and energy from the grid (i.e., GS) for the entire day. The BESS capacity (Cap) and initial energy (IE) are also presented. The daily electricity cost with and without a PV system is presented in Table 4. Because the primary goal is to manage electricity costs in a house by adapting the algorithm to users, three controlling modes are implemented, followed by the user's energy consumption, generation, and BESS status. The PCM enables the management systems to control the BESS when the AD crosses the average demand. The SCM and TCM modes are designed by considering the day-ahead generation and consumption level.
Therefore, the tertiary control mode (TCM) enables the BESS to discharge energy by associating it with the time-varying tariff plan proposed by the power company. The scheduling period of the ESS is predetermined for all three scenarios. Renewable power generation (e.g., PV, wind, and tidal) is strongly dependent on meteorological data. Thus, the probability of changing the power generation volume is high. Given these phenomena, four cases are considered for applying our algorithm: • Scenario 1 ( S 1 ): When the present volume of consumption and generation are higher than day-ahead consumption and generation volume.  Table 7. Percentage of reduction of cost in a single day.   In assessing the robustness of the proposed system, we selected H 1 for S 1 , H 2 for S 2 , H 3 for S 3 , and H 4 for S 4 . Figure 5a illustrates the simulation results of BESS scheduling in terms of energy (kW) against time (hour) for H 1 . The other ordinary axis corresponds to the turn-on and turn-off signals of the deployed BESS. The lower portion represents the charging period and discharging period of each mode, such as the primary discharging period (PDP), the secondary discharging period (SDP), and the tertiary discharging period (TDP). T he TCM always provides maximum discharging allowance for the BESS. The day-ahead forecasted PV power generation (PVG) and forecasted AD (FAD) are also presented. Figure 5b depicts the energy demand, charging and discharging amount, and SoC of BESS in three modes under normal conditions. The discharging amounts of the three different modes are expressed as the primary discharge amount (PDA), the secondary discharge amount (SDA), and the tertiary discharge amount (TDA). Similarly, Fig. 6a illustrates simulation results under optimal conditions, and Fig.6b presents the convergence curve of the proposed optimization model.

PCM (%) SCM (%) TCM (%) PCM (%) SCM (%) TCM (%)
Because the motive of the proposed system is to adjust the scheduling period and the discharge amount of the BESS with the assistance of day-ahead PV power generation and consumption, the proposed optimization model is capable of accomplishing the assignment. As depicted in Fig. 5b, the SoC of the BESS is relatively high such as in the end of the period, despite having a discharge allowance. The final SoC under normal conditions in different modes are PCM-78.43%, SCM-73.00%, and TCM-67.44% while optimal conditions achieve PCM-43.91%, SCM-41.98%, and TCM-36.76%.
After comparing the results, optimal conditions provide a higher discharging allowance within the given constraints. The BESS retains a portion of energy instead of complete discharging caused by insufficient day-ahead PV generation according to the S 1 . Figure 7a illustrates the amount of energy supplied by the grid required to meet the electricity demand at normal and optimal conditions. Similarly, Fig.7b illustrates the volume of BESS energy used by the household. The primary and TCM provides minimum and maximum energy discharging allowance of BESS. These results suggest that the application of the proposed scheme can reduce the supply of grid energy and increase the usage of renewable energy by providing proper coordination among the grid, PV, and BESS system.
We evaluated the proposed system performance by selecting another scenario ( S 4 ) where the sum of day and day-ahead demand and PV generation are approximately equal. The function of the proposed system in the scenario is to fully discharge the BESS under the discharging allowance. Figure 8a demonstrates the scheduling plan for the three control modes by considering the day-ahead generation and demand. The discharge scheduling time for BESS in TCM is longer than discharging allowance in SCM and PCM.
The simulation results of H 4 under normal conditions are depicted in Fig.8b, where the SoCs of the BESS in TCM, SCM, and PCM at the ending period are 58.41% , 66.99% , and 74.11% . Under these conditions, the BESS cannot store excessive generated PV energy because of the higher SoC. Consequently, the system must be designed so that all constraints and dependencies are accomplished. As depicted in Fig.9b, the SoCs in TCM, SCM, and PCM are 33.41% , 28.46% , and 19.97% , which are adequate for improving the system performance in terms of reducing cost and expanding the usages of RES.
The convergence graph and simulation results under optimal conditions are depicted in Fig. 9, and the volume of energy supplied by the grid and BESS is depicted in Fig. 10. Figure 11a,b illustrate the scheduling plan and its day-ahead demand and generation for H 2 and H 3 regarding scenarios S 2 and S 3 . The comparative analysis of providing BESS energy during a single day in different control modes for each household is summarized in Table 5. Table 6 describes the electricity cost of a single day in different optimization modes for both cases [the per-unit cost of the electricity is calculated in the South Korean won ( )]. In Table 7, the performance of the proposed scheme is described in terms of the percentage of cost minimization of a single day.
We also analyzed the payback periods of the proposed system based on the scenarios. We investigated two types of houses to determine payback periods: a conventional house with no PV-BESS installed and a smart house with PV-BESS installed. We calculated the PV and BESS installation costs based on market pricing and used the approximation cost to determine the cost of the proposed control and management system. The overall cost of the proposed control and management system is added to the cost of installing PV-BESS in a conventional Table 9. Payback period analysis of the proposed system with the PV-BESS integrated house. www.nature.com/scientificreports/ home, while the daily energy cost is calculated using a flat-rate tariff. In contrast , only the cost of the proposed control and management system is considered for a smart house, while the daily energy cost is calculated using a dynamic tariff. Tables 8 and 9 represent the conventional and smart house's daily savings and payback periods. The payback period and daily cost savings of a conventional house in the normal mode are greater than even a smart house because the conventional house uses PV-BESS and the proposed scheme simultaneously. Consequently, nearly the entire daily electricity bill can be saved. In contrast, the smart house is already saving money because of the PV-BESS installation and dynamic pricing. Implementing the proposed scheme will further reduce the costs, increasing daily cost savings . Furthermore, because of the lower installation cost of the proposed control and management technology, the smart house's payback periods are shorter than traditional houses in optimization mode. The following explains the total cost analysis and payback times for the three control modes.

PCM.
In PCM optimization mode, the simulation results for the scheduling of the BESS demonstrate a shorter period than in other control modes. The constraints are described in the Eqs. (43) and (45), and the potential supplied energy of the BESS is described by Eqs. (44) and (46). The discharging amount of the BESS is simple and finite and is not dependent on any other factors. From Table 6, the percentage of maximum cost reduction in a single day belongs to H 3 : approximately 12.03% under normal conditions and 33.10% under optimal conditions. The cost reduction difference between normal and optimal condition for H 1 , H 2 , H 3 , and H 4 are 16.42% , 20.18% , 21.07% , and 15.34%.
Furthermore, the higher percentages of daily electricity cost reduction lead the shorter payback periods for the household. While the home is considered a conventional house, the shorter payback periods correspond to H 4 , which is 4 years, 11 months, and 22 days (approximately) under normal conditions and 4 years, 7 months, and 24 days (approximately) under optimal conditions. Furthermore, when the house is deemed a smart house, H 3 has the shortest payback times, which are 7 years and 10 days (approximately) under normal conditions and 2 years, 6 months, and 18 days (approximately) under optimal conditions. SCM. In this case, the constraints for day-ahead power generation are included, and the objective function is A conventional house requires a minimum of 4 years, 9 months, and 14 days (approximately), and a maximum of 5 years and 3 months (approximately) to compensate for the installation cost when their energy demand and PV generation are the same as H 4 and H 1 under normal conditions. In contrast, when the energy consumption and PV generation are the same as H 3 and H 1 under optimal conditions, a smart house requires a minimum of 2 years, 5 months, and 8 days (approximately), and a maximum of 3 years, 11 months, and 16 days (approximately) to recover the installation cost. Consequently, under optimal conditions, smart homes gain a significant improvement in payback time because of lower installation costs.
TCM. The highest optimization scheme of the proposed model is governed by multiple factors and a maximum number of constraints. The dynamic tariff constraint and tertiary discharging factor were added for optimal discharging. Additional constraints in Eqs. (56) and (59) are provided for the control mode. The percentage of minimization of electricity cost is the highest compared with any other model in the system. In this control mode, the highest cost optimization is obtained in H 3 under optimal conditions, and the difference between the two conditions is 19.77% . Similarly, H 1 and H 4 achieve the lowest optimization of about 14.15% and normal optimization of about 15.58% , as anticipated.
Under the TCM, the proposed system provides much shorter payback periods for the households both conventional and smart households. Table 8 reveals that any conventional house with the same energy use and PV generation as the households in this study may recoup its installation and operational costs in four to five years (approximately). Similarly, any smart house can recover the operational cost of the proposed control and management scheme within two to four years (approximately).

Conclusion
This paper proposed an energy management model in the home environment considering the operational constraints of appliances, BESS, and dynamic tariff systems. A PV-BESS controller needs reliable forecasting and robust scheduling algorithms to maximize renewable power utilization and minimize daily electricity costs. Introducing artificial intelligence (AI)-based operational dependencies with the optimization algorithm reduces the gap between generation and consumption, enhancing the robustness and performance of scheduling and optimization results.
Numerical analysis and simulation were conducted on the different scenarios, and control mode schemes demonstrated the effectiveness and robustness of the system. The applied day-ahead load forecasting model achieved an optimal performance: 13.69% , 0.017 kW, and 0.029 kW for the MAPE, MSE, and MAE. Similarly, the model for day-ahead PV generation prediction yielded optimal results: 16.421% , 0.0001 kW, and 0.0061 kW for the MAPE, MSE, and MAE.
Moreover, the maximum improvement is observed in the case of TCM under optimal conditions, while the proposed system is capable of reducing the maximum to approximately 38.77% (under S 3 ) and a minimum of 27.66% (under S 1 ) for daily electricity cost. Therefore, it achieves the highest and lowest utilization of BESS energy of approximately 41.19% (under S 3 ) and 31.59% (under S 1 ) of daily energy demand, resulting in significant Scientific Reports | (2022) 12:15133 | https://doi.org/10.1038/s41598-022-19147-y www.nature.com/scientificreports/ performance under the complexity and constraints inherent in HEMS. In the future, a study will be conducted to improve on the present formulation by including congestion management, which will incorporate PV, wind, and numerous BESSs. Another target of the future research is to implement the proposed system at the hardware level.

Data availability
The data that support the findings of this study are available from Information Technology Research Center (ITRC) but restrictions apply to the availability of these data, which were used under license for the current study, and so are not publicly available. Data are however available from the authors upon reasonable request and with permission of ITRC.